﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GreenTeam.SharePointOnline.BookCompanion.Authentication;
using System.Xml;
using Microsoft.SharePoint.Client;

namespace GreenTeam.SharePointOnline.BookCompanion.ManagedCOMClient
{
  public partial class ElencoCittaForm : System.Windows.Forms.Form
  {
    public ElencoCittaForm()
    {
      InitializeComponent();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnVisualizzaCitta(object sender, EventArgs e)
    {
      var authenticationHelper = new Authenticator(
        txtUrl.Text, txtUser.Text, txtPassword.Text);

      var context = new ClientContext(txtUrl.Text);
      context.ExecutingWebRequest += authenticationHelper.ExecutingWebRequestCallback;
      
      Web web = context.Web;
      ListCollection lists = web.Lists;
      List list = lists.GetByTitle("Citta");
      ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());

      context.Load(listItems);
      context.ExecuteQuery();

      this.DisplayListItems(listItems);
    }

    private void DisplayListItems(ListItemCollection listItems)
    {
      lvCitta.Items.Clear();
      foreach (ListItem item in listItems)
      {
        lvCitta.Items.Add(
          new ListViewItem(new String[] 
          {
            item["Title"].ToString(),
            item["Regione"].ToString(),
            item["Latitudine"].ToString(),
            item["Longitudine"].ToString()
          }));
      }
    }
  }
}
